package com.kdtech.analyse.news;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://news.qq.com/ qq
 * @author allen
 */
public class QqNewsAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*[.]qq.com/.*/[0-9]{8}/.*.htm",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div#C-Main-Article-QQ > div.hd > h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div#Main-P-QQ > div.hd > h1").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("div#ArticleTit").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("html body#v-Article-QQ div#playerOut.wrap div#playerIn.inner h1").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("title").text();
						title=StringUtils.substringBefore(title, "_");
					}
				}
			}
		}
		doc.select("a[_fcksavedurl],div.ft,div#sideBars").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#Cnt-Main-Article-QQ"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#ArticleCnt"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#v-Artic"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#infoTxt p"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#infoCon"));
					}
				}
			}
		}


		date=DateUtils.matchDate(doc.select("span.article-time").text());
		if(date==null){
			if(date==null){
				date=DateUtils.matchDate(doc.select("span.pubTime").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div#ArtFrom").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("div.v-info span#V-time.time").text());
						if(date==null){
							//js中 pubTime="2013年05月29日07:41"
							date=DateUtils.matchDate(StringUtils.substringBetween(htmltxt, "pubTime=\"", "\""));
							if(date==null){
								String substringBetween=StringUtils.substringBetween(htmltxt, "pubtime:'","'");
								if(substringBetween!=null){
									date=DateUtils.matchDate(substringBetween);
								}
								if(date==null){
									date=DateUtils.matchDate(url);
								}
							}
						}
					}
				}
			}
		}

		if(StringUtils.isBlank(title)||date==null){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);

		String author=null;
		Elements select2=doc.select("span[bosszone=jgname]");
		if(select2!=null && select2.size()!=0){
			author=select2.get(0).text();
		}
		news.setAuthor(author);


		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		QqNewsAnalyse a=new QqNewsAnalyse();
		String url="http://news.qq.com/a/20160425/031861.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
//		QqNewsAnalyse.getLinks("http://news.qq.com/a/20151105/040170.htm");
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
